﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.XtraBars.ToolbarForm;
using DevExpress.XtraTreeList.Columns;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using XCI.Helper;
using XCI.Setting.Controls;
using XCI.Windows;

namespace XCI.Setting
{
    public partial class FrmSettingTreeForm : ToolbarForm
    {
        private TreeListPlus _treeListPlus;
        public FrmSettingTreeForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (WinFormHelper.IsDesignMode()) WinFormHelper.InitDesignFont();
        }

        public void Init(TreeListPlus treeListPlus)
        {
            this._treeListPlus = treeListPlus;
            this.barId.EditValue = treeListPlus.Id;
            InitControls();
            TabDelayLoad.Init(tabPane1, name =>
            {
                switch (name)
                {
                    case "tabGeneral":
                        gridGeneral1.Init(treeListPlus);
                        break;
                    case "tabColumns":
                        gridColumns1.Init();
                        break;
                    case "tabAppearance":
                        gridAppearance1.Init(treeListPlus.Appearance);
                        break;
                    case nameof(tabBands):
                        settingTreeBands1.Init(treeListPlus);
                        break;
                    case "tabExpression":
                        treeFormatCondition1.Init();
                        break;
                    case "tabTemplate":
                        gridTemplate1.Init(treeListPlus.SettingManager, "tree_:_");
                        break;
                    case "tabAdvanced":
                        gridAdvanced1.SelectedObject = treeListPlus;
                        break;
                }
            });
        }

        private void InitControls()
        {
            #region gridColumns1
            gridColumns1.GetModelInfo = () => _treeListPlus.ApiModelInfo;
            gridColumns1.GetColumns = () =>
            {
                var colList = new List<TreeListColumn>();
                var hiddenList = new List<TreeListColumn>();
                foreach (TreeListColumn item in _treeListPlus.Columns)
                {
                    if (item.Visible)
                    {
                        colList.Add(item);
                    }
                    else
                    {
                        hiddenList.Add(item);
                    }
                }
                colList.Sort((a, b) => a.VisibleIndex.CompareTo(b.VisibleIndex));
                colList.AddRange(hiddenList);
                return colList;
            };
            gridColumns1.AddColumn = () =>
            {
                var col = new TreeListColumn { Name = $"tcol_{StringHelper.Guid()}", Visible = true };
                col.OptionsColumn.AllowEdit = false;
                // col.AppearanceCell.Options.UseTextOptions = true;
                // col.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                // col.AppearanceHeader.Options.UseTextOptions = true;
                // col.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
                _treeListPlus.Columns.Add(col);
            };
            gridColumns1.RemoveColumn = col =>
            {
                _treeListPlus.Columns.Remove((TreeListColumn)col);
            };
            gridColumns1.AutoBuildColumn = () => _treeListPlus.AutoBuildColumn();
            #endregion

            #region gridFormatCondition1
            treeFormatCondition1.GetConditions = () => _treeListPlus.FormatConditions;
            treeFormatCondition1.AddCondition = () =>
            {
                var condition = new DevExpress.XtraTreeList.StyleFormatConditions.StyleFormatCondition();
                condition.Condition = DevExpress.XtraGrid.FormatConditionEnum.Expression;
                _treeListPlus.FormatConditions.Add(condition);
                return condition;
            };
            treeFormatCondition1.GetColumnInfos = () =>
            {
                var colInfos = new List<ColumnInfo>();
                foreach (TreeListColumn col in _treeListPlus.Columns)
                {
                    string name = string.IsNullOrEmpty(col.Caption) ? col.FieldName : col.Caption;
                    colInfos.Add(new ColumnInfo { Name = name, Column = col });
                }
                return colInfos;
            };
            #endregion
        }

        private void Form_Load(object sender, EventArgs e)
        {
            tabGeneral.BackColor = tabAdvanced.BackColor = this.BackColor;
        }

        private async void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要重置表格配置吗？重置后此表格的配置会全部删除") != DialogResult.Yes) return;

            await _treeListPlus.SettingManager.DeleteAllAsync();
            this.Close();
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            await _treeListPlus.SettingManager.SaveCurrentAsync();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void btnRestoreDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要恢复表格默认配置吗？恢复会丢失当前配置") != DialogResult.Yes) return;

            await _treeListPlus.SettingManager.LoadDefaultAsync();
            MessageBoxHelper.ShowMessage("恢复表格默认配置成功,请点保存确认");
            this.Close();
        }

        private async void btnSaveDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要把当前配置设为默认配置吗？") != DialogResult.Yes) return;

            await _treeListPlus.SettingManager.SaveDefaultAsync();
            MessageBoxHelper.ShowMessage("默认配置设置成功");
        }
    }
}